home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample PMSAM / PMSAM Framework / RoboSamSlot / CLinkedList.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  2.6 KB  |  157 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CLinkedList.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Tim Harnett
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <1>      2/6/95    TMH        new
  13.                   2/6/95    TMH        xxx put comment here xxx
  14.  
  15.     To Do:
  16. */
  17.  
  18. #ifndef __Debug__
  19. #include "Debug.h"
  20. #endif
  21.  
  22. #ifndef __CLinkedList__
  23. #include "CLinkedList.h"
  24. #endif
  25.  
  26.  
  27. //-----------------------
  28. //    C L i n k e d L i s t
  29. //----------------------
  30.  
  31.  
  32. //------------------------------------------------------------------------
  33. void CLinkedList::LinkTail(CLink* item)
  34. {
  35.  
  36.     if( fTail == 0 ) {
  37.     
  38.         ASSERT(fHead==0);
  39.         fTail = item;
  40.         fHead = item;
  41.         
  42.     } else {
  43.         
  44.         ASSERT(fHead != 0 );
  45.         fTail->fLink = item;
  46.         fTail = item;
  47.         
  48.     }
  49.  
  50.     fNItems++;
  51.     item->fLink = 0;
  52.      
  53. }
  54.  
  55.  
  56. //------------------------------------------------------------------------
  57. CLink* CLinkedList::UnlinkHead()
  58. {
  59.  
  60.     CLink* unlinkedHead = 0;
  61.  
  62.     
  63.     if( fNItems != 0 ) {
  64.     
  65.         ASSERT((fHead!=0) && (fTail!=0));
  66.         
  67.         unlinkedHead = fHead;
  68.         
  69.         fHead = unlinkedHead->fLink;
  70.         
  71.         if( fHead == 0 ) {
  72.         
  73.             ASSERT( fTail == unlinkedHead );        // removed the last item.
  74.             fTail = 0;
  75.             
  76.         }
  77.  
  78.  
  79.         fNItems--;
  80.         ASSERT(fNItems >= 0 );
  81.  
  82.     }
  83.     
  84.     return unlinkedHead;
  85.     
  86.      
  87. }
  88.  
  89.  
  90. //-----------------------------------------
  91. //    C L i n k e d L i s t I t e r a t o r
  92. //------------------------------------------
  93.  
  94.  
  95. void CLinkedListIterator::UnlinkCurrentItem() 
  96. {    
  97.     if( fCurrItem == fLinkedList->fTail ) {        // last item in list
  98.  
  99.  
  100.         ASSERT(fNextItem == 0 );
  101.         
  102.         if( fCurrItem == fLinkedList->fHead ) {    // the only item in list
  103.         
  104.             ASSERT(fPrevItem == 0 );
  105.             
  106.             fLinkedList->fHead = 0;
  107.             fLinkedList->fTail = 0;                    //    the list now empty
  108.             fCurrItem = 0;
  109.  
  110.         } else {
  111.         
  112.             ASSERT(fPrevItem != 0);
  113.             
  114.             ASSERT(fPrevItem->fLink == fCurrItem);
  115.             fPrevItem->fLink = 0;        //    previous item is now the
  116.             fLinkedList->fTail = fPrevItem;            //    last item in list
  117.             
  118.             fCurrItem = fPrevItem;
  119.             
  120.         }
  121.         
  122.     } else if( fCurrItem == fLinkedList->fHead ) {    // the first item in list
  123.  
  124.         ASSERT(fCurrItem->fLink == fNextItem );
  125.  
  126.         if( fCurrItem == fLinkedList->fTail ) {        // the only item in the list 
  127.         
  128.             ASSERT(fPrevItem == 0 );
  129.             fLinkedList->fHead = 0;
  130.             fLinkedList->fTail = 0;                    //    the list now empty
  131.             fCurrItem = 0;
  132.             
  133.         } else {
  134.         
  135.             ASSERT(fPrevItem == 0 );                //    the next item becomes
  136.             fLinkedList->fHead = fCurrItem->fLink;    //    the first item in the list
  137.             fNextItem = fLinkedList->fHead;
  138.             fCurrItem = 0;
  139.         }
  140.     
  141.     } else {        // removing a middle item
  142.     
  143.         ASSERT(fPrevItem != 0 );
  144.         ASSERT(fPrevItem->fLink == fCurrItem );
  145.  
  146.         fPrevItem->fLink = fCurrItem->fLink;
  147.         
  148.         fCurrItem = 0;
  149.         
  150.     }
  151.  
  152.     fLinkedList->fNItems--;
  153.     
  154.  
  155. }
  156.  
  157.